package org.lq.er.view.register;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Student;
import org.lq.er.service.StudentService;
import org.lq.er.service.impl.StudentServiceImpl;
import org.lq.er.view.login.V_login;
import org.w3c.dom.css.RGBColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class V_register extends JFrame {

	private JPanel contentPane;
	private JTextField txt_id;
	private JTextField txt_class;
	private JTextField txt_name;
	private JTextField txt_cardId;
	private JTextField txt_phone;
	private JPasswordField txt_pwd;
	StudentService ss = new StudentServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_register frame = new V_register();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_register() {
		setTitle("学生注册");
		Image image = java.awt.Toolkit.getDefaultToolkit().getImage("image/register.jpg");
		this.setIconImage(image);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 788, 464);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		ImageIcon icon = new ImageIcon("image/registerbk.png");
		
		JLabel lblNewLabel = new JLabel(icon);
		lblNewLabel.setBounds(10, 10, 450, 405);
		contentPane.add(lblNewLabel);
		
		JPanel panel = new JPanel();
		panel.setBounds(470, 10, 292, 405);
		panel.setBackground(Color.white);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("欢迎");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 17));
		lblNewLabel_1.setBounds(41, 10, 40, 34);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("请填入基本信息进行注册");
		lblNewLabel_2.setBounds(91, 16, 191, 25);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("学号");
		lblNewLabel_3.setBounds(10, 54, 40, 25);
		panel.add(lblNewLabel_3);
		
		JLabel lblNewLabel_3_1 = new JLabel("班级");
		lblNewLabel_3_1.setBounds(10, 91, 40, 25);
		panel.add(lblNewLabel_3_1);
		
		JLabel lblNewLabel_3_2 = new JLabel("姓名");
		lblNewLabel_3_2.setBounds(10, 136, 40, 25);
		panel.add(lblNewLabel_3_2);
		
		JLabel lblNewLabel_3_3 = new JLabel("身份证号");
		lblNewLabel_3_3.setBounds(10, 184, 53, 25);
		panel.add(lblNewLabel_3_3);
		
		JLabel lblNewLabel_3_4 = new JLabel("密码");
		lblNewLabel_3_4.setBounds(10, 231, 40, 25);
		panel.add(lblNewLabel_3_4);
		
		JLabel lblNewLabel_3_5 = new JLabel("手机号");
		lblNewLabel_3_5.setBounds(10, 277, 40, 25);
		panel.add(lblNewLabel_3_5);
		
		txt_id = new JTextField();
		txt_id.setBounds(89, 56, 193, 21);
		panel.add(txt_id);
		txt_id.setColumns(10);
		
		txt_class = new JTextField();
		txt_class.setColumns(10);
		txt_class.setBounds(89, 93, 193, 21);
		panel.add(txt_class);
		
		txt_name = new JTextField();
		txt_name.setColumns(10);
		txt_name.setBounds(89, 138, 193, 21);
		panel.add(txt_name);
		
		txt_cardId = new JTextField();
		txt_cardId.setColumns(10);
		txt_cardId.setBounds(89, 186, 193, 21);
		panel.add(txt_cardId);
		
		txt_phone = new JTextField();
		txt_phone.setColumns(10);
		txt_phone.setBounds(89, 279, 193, 21);
		panel.add(txt_phone);
		
		txt_pwd = new JPasswordField();
		txt_pwd.setBounds(89, 233, 193, 23);
		panel.add(txt_pwd);
		
		JButton btnNewButton = new JButton("提交");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean flag = false;
				Student s = new Student();
				try {
					s.setStu_no(Integer.parseInt(txt_id.getText().toString().trim()));
					s.setClasses(txt_class.getText().toString().trim());
					s.setName(txt_name.getText().toString().trim());
					s.setPassword(txt_pwd.getText().toString().trim());
					s.setCard_id(txt_cardId.getText().toString().trim());
					s.setPhone_no(txt_phone.getText().toString().trim());
					flag = ss.save(s);
				} catch (Exception e1) {
				}
				
				if(flag) {
					JOptionPane.showMessageDialog(V_register.this, "恭喜您，注册成功，快去登录吧(*^_^*)", "提示", JOptionPane.INFORMATION_MESSAGE);
					dispose();
					V_login login = new V_login();
					login.setVisible(true);
				}
				else {
					JOptionPane.showMessageDialog(V_register.this, "很遗憾，注册失败", "错误", JOptionPane.ERROR_MESSAGE);
					reset();
				}
			}
		});
		btnNewButton.setForeground(Color.WHITE);
		btnNewButton.setBounds(27, 339, 77, 34);
		btnNewButton.setBackground(new Color(208,32,144));
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				reset();
			}
		});
		btnNewButton_1.setForeground(Color.WHITE);
		btnNewButton_1.setBackground(new Color(208, 32, 144));
		btnNewButton_1.setBounds(185, 339, 77, 34);
		panel.add(btnNewButton_1);
		
	}
	public void reset() {
		txt_id.setText("");
		txt_class.setText("");
		txt_name.setText("");
		txt_pwd.setText("");
		txt_pwd.setText("");
		txt_cardId.setText("");
	}
}
